package com.spzx.product.controller;

import com.spzx.common.core.web.controller.BaseController;
import com.spzx.common.core.web.domain.AjaxResult;
import com.spzx.common.core.web.page.TableDataInfo;
import com.spzx.product.api.domain.Brand;
import com.spzx.product.domain.CategoryBrand;
import com.spzx.product.service.ICategoryBrandService;
import com.spzx.product.service.ICategoryService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

/**
 * @Author ChenXing
 * @Description
 * @Create 2024/7/28 17:16
 */

@Tag(name = "分类品牌接口管理")
@RestController
@RequestMapping("/categoryBrand")
public class CategoryBrandController extends BaseController {

    @Autowired
    private ICategoryBrandService categoryBrandService;

    @Autowired
    private ICategoryService categoryService;

    @Operation(summary = "查询分类品牌列表")
    @GetMapping("/list")
    public TableDataInfo list(CategoryBrand categoryBrand) {
        startPage();
        List<CategoryBrand> list = categoryBrandService.selectCategoryBrandList(categoryBrand);
        return getDataTable(list);
    }

    @Operation(summary = "获取分类品牌详细信息")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        CategoryBrand categoryBrand =categoryBrandService.getById(id) ;
        List<Long> categoryIdList = categoryService.getAllCategoryIdList(categoryBrand.getCategoryId());
        categoryBrand.setCategoryIdList(categoryIdList);
        return success(categoryBrand);
    }

    @Operation(summary = "新增分类品牌")
    @PostMapping
    public AjaxResult add(@RequestBody @Validated CategoryBrand categoryBrand) {

      //  categoryBrand.setCreateBy(SecurityUtils.getUsername());
        boolean b = categoryBrandService.save(categoryBrand);
        return toAjax(b);
    }

    @Operation(summary = "修改分类品牌")
    @PutMapping
    public AjaxResult edit(@RequestBody @Validated CategoryBrand categoryBrand) {
        // 方法1
       //  boolean b = categoryBrandService.saveOrUpdate(categoryBrand);
        // 方法2 加了一个重复判断
        int i = categoryBrandService.updateCategoryBrand(categoryBrand);
        return toAjax(i);
    }

    @Operation(summary = "删除分类品牌")
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(categoryBrandService.removeBatchByIds(Arrays.asList(ids)));
    }

    @Operation(summary = "根据分类id获取品牌列表")
    @GetMapping("/brandList/{id}")
    public AjaxResult remo(@PathVariable Long id) {
       List<Brand> brandList = categoryBrandService.getBrandListByCategory(id);
       return  success(brandList);
    }
}
